package com.mtjsoft.www.kotlinmvputils

import android.app.Application
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Color
import android.os.Build
import android.os.StrictMode
import androidx.multidex.MultiDex
import com.alibaba.android.arouter.launcher.ARouter
import com.mtjsoft.www.kotlinmvputils.imp.IComponentApplication
import com.mtjsoft.www.kotlinmvputils.manager.AndBaseTopViewInfo
import com.mtjsoft.www.kotlinmvputils.utils.KLog
import com.tencent.mmkv.MMKV
import okhttp3.OkHttpClient
import rxhttp.wrapper.param.RxHttp
import java.util.concurrent.TimeUnit

abstract class BaseApplication : Application() {

    private val modulesList = arrayListOf(
        "com.suntront.module_arcgis.appliction.ModuleApp",
        "com.suntront.module_bluetooth.appliction.ModuleApp",
        "com.suntront.module_datasynch.appliction.ModuleApp",
        "com.suntront.module_login.ModuleApp",
        "com.suntront.module_mine.ModuleApp",
        "com.suntront.module_metertask.ModuleApp"
    )
    private var okHttpClient: OkHttpClient? = null

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    override fun onCreate() {
        super.onCreate()
        //配置统一标题栏样式
        AndBaseTopViewInfo.titleSize = 16
        AndBaseTopViewInfo.titleMode = AndBaseTopViewInfo.TitleMode.CENTER
        AndBaseTopViewInfo.titleTextColor = Color.WHITE
        AndBaseTopViewInfo.backgroundColor = R.color.main_base_color
        AndBaseTopViewInfo.backLeftDrawable = R.drawable.ic_arrow_white_24dp
        //统一处理7.0文件路径问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val svb = StrictMode.VmPolicy.Builder()
            StrictMode.setVmPolicy(svb.build())
        }
        if (BuildConfig.DEBUG) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        KLog.init(BuildConfig.DEBUG)
        ARouter.init(this)
        MMKV.initialize(this)
        if (okHttpClient == null) {
            okHttpClient = OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build()
            RxHttp.init(okHttpClient, BuildConfig.DEBUG)
        }
        //Module类的APP初始化
        modulesApplicationInit()
    }

    /**
     * 使用反射
     * 初始化Module类的APP
     */
    private fun modulesApplicationInit() {
        for (moduleImpl in modulesList) {
            try {
                val clazz = Class.forName(moduleImpl)
                val obj = clazz.newInstance()
                if (obj is IComponentApplication) {
                    obj.init(this)
                }
            } catch (e: ClassNotFoundException) {
                e.printStackTrace();
            } catch (e: IllegalAccessException) {
                e.printStackTrace();
            } catch (e: InstantiationException) {
                e.printStackTrace();
            }
        }
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        if (newConfig.fontScale != 1f) {
            resources
        }
        super.onConfigurationChanged(newConfig)
    }

    override fun getResources(): Resources? {
        val res: Resources = super.getResources()
        if (res.configuration.fontScale != 1f) {
            val newConfig = Configuration()
            newConfig.setToDefaults()
            res.updateConfiguration(newConfig, res.displayMetrics)
        }
        return res
    }
}